$(function () {
    $("form").validate({
        submitHandler: function (form) { // 必须写在验证前面，否则无法ajax提交

            ly.ajaxSubmit(form, { // 验证新增是否成功
                type: "post",
                dataType: "json",
                success: function (data) {
                    if (data == "success") {
                        layer.confirm('添加成功!是否关闭窗口?', function (index) {
                            parent.grid.loadData();
                            parent.layer.close(parent.pageii);
                            return false;
                        });
                        $("#form")[0].reset();
                    } else {
                        layer.alert('添加失败！', 3);
                    }
                }
            });
        },
        rules: {
            "tongueStandardFormMap.title": {
                required: true
            },
            "tongueStandardFormMap.path": {
                required: true
            },
            "tongueStandardFormMap.content": {
                required: true
            }
        },
        messages: {
            "tongueStandardFormMap.title": {
                required: "请输入标题"
            } ,
            "tongueStandardFormMap.path": {
                required: "请上传图片"
            },
            "tongueStandardFormMap.content": {
                required: "请输入内容"
            }
        },
        errorPlacement: function (error, element) { // 自定义提示错误位置
            $(".l_err").css('display', 'block');
            // element.css('border','3px solid #FFCCCC');
            $(".l_err").html(error.html());
        },
        success: function (label) { // 验证通过后
            $(".l_err").css('display', 'none');
        }
    });

    $('.system-upload-field').change(function () {
        if ($(this).val() != '') {
            var dataFor = $(this).attr('data-for');
            var formData = new FormData();
            formData.append("file", $(this)[0].files[0]);
            $.ajax({
                url: rootPath + '/system/upload.shtml',
                type: 'POST',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                success: function (res) {
                    $('[name="' + dataFor + '"]').val(eval(res));
                },
                error: function (err) {
                    alert("图片上传失败");
                }
            });
        }
    });
});

